<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #first_code="{record}">
        <div class="flex" style="width: 100%;">
          <div class="flex_1" style="text-align: center;">{{ record.first_code }}</div>
          <div class="flex_1" style="text-align: center;">{{ record.second_code }}</div>
          <div class="flex_1" style="text-align: center;">{{ record.third_code }}</div>
        </div>
      </template>
      <template #tableButtons>
        <a-button type="primary" status="danger" @click="handleShow(1,{})">新 增</a-button>
      </template>
      <template #operationBeforeExtend="{record}">
        <a-link @click="handleShow(2,record)">编辑</a-link>
      </template>
      
    </ma-crud>

    <!-- 新增/编辑 -->
    <a-modal :mask-closable="false"  v-model:visible="showAdd" @cancel="showAdd = false" :width="800" :title='showType == 1?"新增":"编辑"' :on-before-ok="handleOkssss">
      <!-- <div class="flex align_center" style="margin-bottom: 2%;">
        <div style="width: 80px;">代码：</div>
        <div class="flex">
          <a-input v-model="formData.first_code" style="width: 80px;margin-right:12px" placeholder="代码" allow-clear  />
          <a-input v-model="formData.second_code" style="width: 80px;margin-right:12px" placeholder="代码" allow-clear  />
          <a-input v-model="formData.third_code" style="width: 80px;" placeholder="代码" allow-clear  />
        </div>
      </div> -->
      <div class="flex align_center" style="margin-bottom: 2%;">
        <div style="width: 80px;">品类名称：</div>
        <div class="flex">
          <a-input v-model="formData.name" style="width: 264px;margin-right:12px" placeholder="品类名称" allow-clear  />
        </div>
      </div>
    </a-modal>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import goodsType from '@/api/goods/goodsType'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const crudRef = ref()
const showAdd = ref(false)
const showType = ref()

const formData = ref({
      first_code:'',
      second_code:'',
      third_code:'',
      name:'',
})

const handleShow = (type,item)=>{
  
  showType.value = type
  if(type == 1){
    formData.value = {
      first_code:'',
      second_code:'',
      third_code:'',
      name:'',
    }
  }else{
    formData.value = {
      first_code:item.first_code,
      second_code:item.second_code,
      third_code:item.third_code,
      name:item.name,
      id:item.id
    }
  }
  showAdd.value = true
}

const handleOkssss = async ()=>{
  if(showType.value == 1){   // 新增
    let res = await goodsType.save(formData.value)
    if(res.code == 200){
      Message.success('新增成功！')
      crudRef.value.refresh()
      return true
    }else{
      Message.error(res.message)
      return false
    }
  }else{  // 编辑
    let res = await goodsType.update(formData.value.id,formData.value)
    if(res.code == 200){
      Message.success('修改成功！')
      crudRef.value.refresh()
      return true
    }else{
      Message.error(res.message)
      return false
    }
  }
}

const numberOperation = (newValue, id, numberName) => {
  goodsType.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  goodsType.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'flyos_goods_type',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: goodsType.getList,
  recycleApi: goodsType.getRecycleList,
  add: {
    show: false,
    api: goodsType.save,
    auth: ['goods:type:save']
  },
  edit: {
    show: false,
    api: goodsType.update,
    auth: ['goods:type:update']
  },
  delete: {
    show: true,
    api: goodsType.deletes,
    auth: ['goods:type:delete'],
    realApi: goodsType.realDeletes,
    realAuth: ['goods:type:realDeletes']
  },
  recovery: {
    show: false,
    api: goodsType.recoverys,
    auth: ['goods:type:recovery']
  },
  import: {
    show: false,
    url: 'goods/type/import',
    templateUrl: 'goods/type/downloadTemplate',
    auth: ['goods:type:import']
  },
  export: {
    show: false,
    url: 'goods/type/export',
    auth: ['goods:type:export']
  }
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  // {
  //   title: "代码",
  //   dataIndex: "first_code",
  //   formType: "input",
  //   align:'center',
  // },
  {
    title: "品类名称",
    dataIndex: "name",
    formType: "input",
    search: true
  },
  
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
</script>
<script> export default { name: 'goods:type' } </script>